package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
	midjouneryServer "hhos-mdijounery-server/proto"
	"hhos-mdijounery-server/utils"
	"path/filepath"
	"runtime"
)

func Path(rel string) string {
	var basePath string
	_, currentFile, _, _ := runtime.Caller(0)
	basePath = filepath.Dir(currentFile)

	if filepath.IsAbs(rel) {
		return rel
	}

	return filepath.Join(basePath, rel)
}

func main() {

	cred, err := credentials.NewClientTLSFromFile(Path("../config/ca_cert.pem"), "*.test.example.com")
	if err != nil {
		fmt.Println("err:", err)
	}

	options := []grpc.DialOption{
		grpc.WithTransportCredentials(cred),
	}

	conn, err := grpc.Dial("127.0.0.1:50051", options...)
	if err != nil {
		fmt.Println("did not connection:", err)
	}
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println("conn.Close-err:", err)
		}
	}(conn)

	// 建立连接
	client := midjouneryServer.NewMidjourneyClient(conn)

	// 连接请求文生图
	clientImagine(client)

	//连接请求绘画改变
	//clientChange(client)
}

// clientImagine 连接请求文生图
func clientImagine(client midjouneryServer.MidjourneyClient) {
	// 文生图
	response, err := client.Imagine(context.Background(), &midjouneryServer.ImagineRequest{
		Prompt:     "画山水画",
		ImgUrl:     "",
		Idempotent: utils.GetUniqId(),
	})
	if err != nil {
		fmt.Println("response-err:", err)
		return
	}
	fmt.Println("response-idempotent:", response.GetIdempotent())
	fmt.Println("response-taskId:", response.GetTaskId())
}

// clientChange 连接请求绘画改变
func clientChange(client midjouneryServer.MidjourneyClient) {
	// 绘画改变
	response, err := client.Change(context.Background(), &midjouneryServer.ChangeRequest{
		Action:      "UPSCALE",
		Index:       1,
		ImageTaskId: 14,
		Idempotent:  utils.GetUniqId(),
	})

	if err != nil {
		fmt.Println("response-err:", err)
		return
	}

	fmt.Println("response-idempotent", response.Idempotent)
	fmt.Println("response-TaskId", response.TaskId)
}
